#include <math.h>
#include <iostream>

using std::cout;
using std::endl;


class Point {
public:
    Point(int ix = 0, int iy = 0)
    : _ix(ix)
    , _iy(iy)
    {
        cout << "Point(int,int)" << endl;
    }

    void print() const
    {
        cout << "(" << _ix 
             << "," << _iy
             << ")" << endl;
    }

    //类Line声明为Point的友元
    friend class Line;//友元类
    
private:
    int _ix;
    int _iy;
};

class Line {
public:
    Line(int x1, int y1, int x2, int y2)
    : _pt1(x1, y1)
    , _pt2(x2, y2)
    {}

    double distance()
    {
        return sqrt((_pt1._ix - _pt2._ix) * (_pt1._ix - _pt2._ix)  + 
                    (_pt1._iy - _pt2._iy) * (_pt1._iy - _pt2._iy));
    }

    void setX(Point & pt, int ix);
private:
    Point _pt1;//尽管_pt1是Line的对象成员，但_pt1对象内部的私有成员Line是无法直接访问的
    Point _pt2;//必须要将Line设计成Point的友元之后，才能直接访问
};


void Line::setX(Point & pt, int ix)
{
    pt._ix = ix;
}

int main()
{
    cout << "class as friend:" << endl;
    Line line(1, 2, 3, 4);
    cout << "line中两点的距离是:" << line.distance();
    cout << endl;
    return 0;
}

